package com.yisu.expressway.utils;

import android.view.View;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;

/**
 * Created by Administrator on 2017/1/12 0012.
 */

public class AnimationUtils {
    public static void startVoiceLineAnimation(final boolean showVoice, final View voiceLine,final View voiceIcon){
        float startL = showVoice ? 0.0f : 1.0f ;
        float endL = showVoice ? 1.0f : 0.0f ;

        float startIcon = showVoice ? 1.0f : 0.0f;
        float endIcon = showVoice ? 0.0f : 1.0f;

        final ObjectAnimator showVoiceLine = ObjectAnimator//
                .ofFloat(voiceLine, "showVoiceLine", startL,  endL)
                .setDuration(300);
        showVoiceLine.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                float cVal = (Float) animation.getAnimatedValue();
                voiceLine.setAlpha(cVal);
                voiceLine.setScaleX(cVal);
            }
        });

        final ObjectAnimator showVoiceIcon =  ObjectAnimator//
                .ofFloat(voiceIcon, "showIcon", startIcon,  endIcon)
                .setDuration(300);

        showVoiceIcon.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float cVal = (Float) valueAnimator.getAnimatedValue();
                voiceIcon.setAlpha(cVal);
                voiceIcon.setScaleX(cVal);
                voiceIcon.setScaleY(cVal);
            }
        });

        showVoiceIcon.addListener(new ValueAnimator.AnimatorListener(){

            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                if(showVoice){
                    voiceIcon.setVisibility(View.GONE);
                    showVoiceLine.start();
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        showVoiceLine.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                float cVal = (Float) animation.getAnimatedValue();
                voiceLine.setAlpha(cVal);
                voiceLine.setScaleX(cVal);
            }
        });


        showVoiceLine.addListener(new ValueAnimator.AnimatorListener(){

            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                if(!showVoice){
                    voiceIcon.setVisibility(View.VISIBLE);
                    showVoiceIcon.start();
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        if(showVoice){
            showVoiceIcon.start();
        }else {
            showVoiceLine.start();
        }
    }
}
